package com.kern.academic.util;

import java.io.*;

/**
 *  序列化与反序列化
 * Created by caoshi at 7:49 2021-07-02
 */
public class SerializeUtil {

    private static final String CHARSET_NAME = "ISO-8859-1";

    /**
     * 序列化对象
     * @param obj
     * @return
     * @throws IOException
     */
    public static String serialize(Object obj) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream objOs = new ObjectOutputStream(baos);
        objOs.writeObject(obj);
        String str = baos.toString(CHARSET_NAME);
        objOs.close();
        baos.close();
        return str;
    }

    /**
     * 对象反序列化
     * @param str
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Object deserialize(String str) throws IOException, ClassNotFoundException {
        ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes(CHARSET_NAME));
        ObjectInputStream objIs = new ObjectInputStream(bis);
        Object object = objIs.readObject();
        objIs.close();
        bis.close();
        return object;
    }




}
